<?php
	class dathang_chitietdb
	{
		public function GetList($MaChiTiet = 0, $MaDatHang = 0, $order = '', $start = 0, $num = 30, $all = 0)
		{
			$sql =	"SELECT * ";
			$sql.=	"FROM tbl_dathang H, tbl_dathang_chitiet C, tbl_sach S ";
			$sql.=	"WHERE H.MaDatHang = C.MaDatHang AND C.MaSach = S.MaSach";
			
			if ($MaChiTiet > 0)
				$sql .= " AND MaChiTiet = " . dataservice::QuoteSmart($MaChiTiet);
			
			if ($MaDatHang > 0)
				$sql .= " AND MaDatHang = " . dataservice::QuoteSmart($MaDatHang);
			
			if ($order != '')
				$sql .= " ORDER BY " . $order . " DESC";
			
			if($all == 0)
				$sql .= " LIMIT " . $start . ", ".$num;
			
			$db = new dataservice();
			$ret = $db->ExecuteQuery($sql);
			return $ret;
		}
		
		public function Add($MaGioHang = 0, $MaSanPham = 0, $SoLuong = 0)
		{
			$sql = "INSERT INTO tbl_chitietgiohang(MaGioHang, MaSanPham, SoLuong) VALUES(" . dataservice::QuoteSmart($MaGioHang) . ", " . dataservice::QuoteSmart($MaSanPham) . ", " . dataservice::QuoteSmart($SoLuong) . ")";
			
			$db = new dataservice();
			return $db->ExecuteNonQuery($sql);
		}
		
		public function Delete($MaChiTiet = 0)
		{
			$sql = "DELETE FROM tbl_dathang_chitiet WHERE (1)";
			
			if ($MaChiTiet > 0)
				$sql .= " AND MaChiTiet = " . dataservice::QuoteSmart($MaChiTiet);
			
			$db = new dataservice();
			$ret = $db->ExecuteNonQuery($sql);
			return $ret;
		}
		
		public function CountList()
		{
			$sql = "SELECT COUNT(MaChiTiet) AS SoLuongCTDatHang FROM tbl_dathang_chitiet WHERE (1)";
			
			$db = new dataservice();
			$ret = $db->ExecuteQuery($sql);
			return $ret;
		}
	}
?>